// Copyright 2004-2006 Castle Project - http://www.castleproject.org/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if DOTNET2
namespace Adge.Models.Tests
{
    using System;
    using System.Text;
    using System.Reflection;
    using System.Collections.Generic;

    using NUnit.Framework;

    using NHibernate.Expression;
    using Adge.Models;
    using Castle.ActiveRecord;

    [TestFixture]
    public class ActiveRecordBaseGenericsTestCase : AbstractActiveRecordTest
    {
        [SetUp]
        public void Setup()
        {
            ActiveRecordStarter.ResetInitializationFlag();

            ActiveRecordStarter.Initialize(GetConfigSource(),
                //typeof(Blog),
                //typeof(Post)
                typeof(Adge.Models.ArticleInfo),
                typeof(Adge.Models.CategoryInfo),
                typeof(Adge.Models.MagazineInfo),
                typeof(Adge.Models.MenuInfo),
                typeof(Adge.Models.NewsInfo),
                typeof(Adge.Models.SubjectInfo)
                );

            Recreate();
            //Post.DeleteAll();
            //Blog.DeleteAll();
            NewsInfo.DeleteAll();
            CategoryInfo.DeleteAll();
            ArticleInfo.DeleteAll();
            MagazineInfo.DeleteAll();
            MenuInfo.DeleteAll();
            SubjectInfo.DeleteAll();
        }

        //[Test]
        //public void SimpleOperations()
        //{
        //    Blog[] blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(0, blogs.Length);

        //    Blog blog = new Blog();
        //    blog.Name = "hammett's blog";
        //    blog.Author = "hamilton verissimo";
        //    blog.Save();

        //    blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(1, blogs.Length);

        //    Blog retrieved = blogs[0];
        //    Assert.IsNotNull(retrieved);

        //    Assert.AreEqual(blog.Name, retrieved.Name);
        //    Assert.AreEqual(blog.Author, retrieved.Author);
        //}

        [Test]
        public void AdgeSimpleOperations()
        {
            CategoryInfo catlog = new CategoryInfo();
            catlog.CatDesc = "just for test";
            catlog.CatLayer = "000002";
            catlog.Catname = "test";
            catlog.CatParentId = 0;
            if (CategoryInfo.TryFind(1) == null)
            {
                catlog.Save();
            }


            NewsInfo[] blogs = NewsInfo.FindAll();

            Assert.IsNotNull(blogs);
            Assert.AreEqual(0, blogs.Length);

            NewsInfo newsinfo = new NewsInfo();
            newsinfo.Title = "hammett's blog";
            newsinfo.Author = "hamilton verissimo";
            newsinfo.AdgeInfoCategory = catlog;
            newsinfo.Content = "test content aha.";
            newsinfo.LastUpdateTime = DateTime.Now;
            newsinfo.PageViewed = 0;
            newsinfo.PubDate = new DateTime(2006, 10, 20);
            newsinfo.Visible = true;
            newsinfo.Save();

            blogs = NewsInfo.FindAll();

            Assert.IsNotNull(blogs);
            Assert.AreEqual(1, blogs.Length);

            NewsInfo retrieved = blogs[0];
            Assert.IsNotNull(retrieved);

            Assert.AreEqual(newsinfo.Title, retrieved.Title);
            Assert.AreEqual(newsinfo.Author, retrieved.Author);
        }

        //[Test]
        //public void SlicedOperation()
        //{
        //    Blog blog = new Blog();
        //    blog.Name = "hammett's blog";
        //    blog.Author = "hamilton verissimo";
        //    blog.Save();

        //    Post post1 = new Post(blog, "title1", "contents", "category1");
        //    Post post2 = new Post(blog, "title2", "contents", "category2");
        //    Post post3 = new Post(blog, "title3", "contents", "category3");

        //    post1.Save();
        //    post2.Save();
        //    post3.Published = true;
        //    post3.Save();
        //    Post[] posts = Post.CustomSlicedFind(Expression.Eq("Blog", blog), 1, 2);
        //    Assert.AreEqual(2, posts.Length);
        //}

        //[Test]
        //public void SimpleOperations2()
        //{
        //    Blog[] blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(0, blogs.Length);

        //    Blog blog = new Blog();
        //    blog.Name = "hammett's blog";
        //    blog.Author = "hamilton verissimo";
        //    blog.Create();

        //    blogs = Blog.FindAll();
        //    Assert.AreEqual(blog.Name, blogs[0].Name);
        //    Assert.AreEqual(blog.Author, blogs[0].Author);

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(1, blogs.Length);

        //    blog.Name = "something else1";
        //    blog.Author = "something else2";
        //    blog.Update();

        //    blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(1, blogs.Length);
        //    Assert.AreEqual(blog.Name, blogs[0].Name);
        //    Assert.AreEqual(blog.Author, blogs[0].Author);
        //}

        //[Test]
        //public void ComponentAttribute()
        //{
        //    Company company = new Company("Castle Corp.");
        //    company.Address = new PostalAddress(
        //        "Embau St., 102", "Sao Paulo", "SP", "040390-060");
        //    company.Save();

        //    Company[] companies = Company.FindAll();
        //    Assert.IsNotNull(companies);
        //    Assert.AreEqual(1, companies.Length);

        //    Company corp = companies[0];
        //    Assert.IsNotNull(corp.Address);
        //    Assert.AreEqual(corp.Address.Address, company.Address.Address);
        //    Assert.AreEqual(corp.Address.City, company.Address.City);
        //    Assert.AreEqual(corp.Address.State, company.Address.State);
        //    Assert.AreEqual(corp.Address.ZipCode, company.Address.ZipCode);
        //}

        //[Test]
        //public void RelationsOneToMany()
        //{
        //    Blog blog = new Blog();
        //    blog.Name = "hammett's blog";
        //    blog.Author = "hamilton verissimo";
        //    blog.Save();

        //    Post post1 = new Post(blog, "title1", "contents", "category1");
        //    Post post2 = new Post(blog, "title2", "contents", "category2");

        //    post1.Save();
        //    post2.Save();

        //    blog = Blog.Find(blog.Id);

        //    Assert.IsNotNull(blog);
        //    Assert.IsNotNull(blog.Posts, "posts collection is null");
        //    Assert.AreEqual(2, blog.Posts.Count);

        //    foreach (Post post in blog.Posts)
        //    {
        //        Assert.AreEqual(blog.Id, post.Blog.Id);
        //    }
        //}

        //[Test]
        //public void RelationsOneToManyWithWhereAndOrder()
        //{
        //    Blog blog = new Blog();
        //    blog.Name = "hammett's blog";
        //    blog.Author = "hamilton verissimo";
        //    blog.Save();

        //    Post post1 = new Post(blog, "title1", "contents", "category1");
        //    Post post2 = new Post(blog, "title2", "contents", "category2");
        //    Post post3 = new Post(blog, "title3", "contents", "category3");

        //    post1.Save();
        //    System.Threading.Thread.Sleep(1000); // Its a smalldatetime (small precision)
        //    post2.Save();
        //    System.Threading.Thread.Sleep(1000); // Its a smalldatetime (small precision)
        //    post3.Published = true;
        //    post3.Save();

        //    blog = Blog.Find(blog.Id);

        //    Assert.IsNotNull(blog);
        //    Assert.AreEqual(2, blog.UnPublishedPosts.Count);
        //    Assert.AreEqual(1, blog.PublishedPosts.Count);

        //    Assert.AreEqual(3, blog.RecentPosts.Count);
        //    Assert.AreEqual(post3.Id, (blog.RecentPosts[0] as Post).Id);
        //    Assert.AreEqual(post2.Id, (blog.RecentPosts[1] as Post).Id);
        //    Assert.AreEqual(post1.Id, (blog.RecentPosts[2] as Post).Id);
        //}

        //[Test]
        //public void RelationsOneToOne()
        //{
        //    Employee emp = new Employee();
        //    emp.FirstName = "john";
        //    emp.LastName = "doe";
        //    emp.Save();

        //    Assert.AreEqual(1, Employee.FindAll().Length);

        //    Award award = new Award(emp);
        //    award.Description = "Invisible employee";
        //    award.Save();

        //    Assert.AreEqual(1, Award.FindAll().Length);

        //    Employee emp2 = Employee.Find(emp.ID);
        //    Assert.IsNotNull(emp2);
        //    Assert.IsNotNull(emp2.Award);
        //    Assert.AreEqual(emp.FirstName, emp2.FirstName);
        //    Assert.AreEqual(emp.LastName, emp2.LastName);
        //    Assert.AreEqual(award.Description, emp2.Award.Description);
        //}

        //[Test]
        //[ExpectedException(typeof(NotFoundException))]
        //public void FindLoad()
        //{
        //    Blog.Find(1);
        //}

        //[Test]
        //public void SaveUpdate()
        //{
        //    Blog[] blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(0, blogs.Length);

        //    Blog blog = new Blog();
        //    blog.Name = "hammett's blog";
        //    blog.Author = "hamilton verissimo";
        //    blog.Save();

        //    blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(1, blogs.Length);

        //    blog.Name = "Something else";
        //    blog.Author = "changed too";
        //    blog.Save();

        //    blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(1, blogs.Length);

        //    Assert.AreEqual(blog.Name, blogs[0].Name);
        //    Assert.AreEqual(blog.Author, blogs[0].Author);
        //}

        //[Test]
        //public void Delete()
        //{
        //    Blog[] blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(0, blogs.Length);

        //    Blog blog = new Blog();
        //    blog.Name = "hammett's blog";
        //    blog.Author = "hamilton verissimo";
        //    blog.Save();

        //    blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(1, blogs.Length);

        //    blog.Delete();

        //    blogs = Blog.FindAll();

        //    Assert.IsNotNull(blogs);
        //    Assert.AreEqual(0, blogs.Length);
        //}
    }

}
#endif
